<?php
define("EW_PAGE_ID", "edit", TRUE); // Page ID
define("EW_TABLE_NAME", 'tb_locations', TRUE);
?>
<?php 
session_start(); // Initialize session data
ob_start(); // Turn on output buffering
?>
<?php include "ewcfg50.php" ?>
<?php include "ewmysql50.php" ?>
<?php include "phpfn50.php" ?>
<?php include "tb_locationsinfo.php" ?>
<?php include "userfn50.php" ?>
<?php include "tb_usersinfo.php" ?>
<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // Always modified
header("Cache-Control: private, no-store, no-cache, must-revalidate"); // HTTP/1.1 
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache"); // HTTP/1.0
?>
<?php

// Open connection to the database
$conn = ew_Connect();
?>
<?php
$Security = new cAdvancedSecurity();
?>
<?php
if (!$Security->IsLoggedIn()) $Security->AutoLogin();
if (!$Security->IsLoggedIn()) {
	$Security->SaveLastUrl();
	Page_Terminate("login.php");
}
?>
<?php

// Common page loading event (in userfn*.php)
Page_Loading();
?>
<?php

// Page load event, used in current page
Page_Load();
?>
<?php
$tb_locations->Export = @$_GET["export"]; // Get export parameter
$sExport = $tb_locations->Export; // Get export parameter, used in header
$sExportFile = $tb_locations->TableVar; // Get export file, used in header
?>
<?php

// Load key from QueryString
if (@$_GET["ID"] <> "") {
	$tb_locations->ID->setQueryStringValue($_GET["ID"]);
}

// Create form object
$objForm = new cFormObj();
if (@$_POST["a_edit"] <> "") {
	$tb_locations->CurrentAction = $_POST["a_edit"]; // Get action code
	LoadFormValues(); // Get form values
} else {
	$tb_locations->CurrentAction = "I"; // Default action is display
}

// Check if valid key
if ($tb_locations->ID->CurrentValue == "") Page_Terminate($tb_locations->getReturnUrl()); // Invalid key, exit
switch ($tb_locations->CurrentAction) {
	case "I": // Get a record to display
		if (!LoadRow()) { // Load Record based on key
			$_SESSION[EW_SESSION_MESSAGE] = "No records found"; // No record found
			Page_Terminate($tb_locations->getReturnUrl()); // Return to caller
		}
		break;
	Case "U": // Update
		$tb_locations->SendEmail = TRUE; // Send email on update success
		if (EditRow()) { // Update Record based on key
			$_SESSION[EW_SESSION_MESSAGE] = "Update successful"; // Update success
			Page_Terminate($tb_locations->getReturnUrl()); // Return to caller
		} else {
			RestoreFormValues(); // Restore form values if update failed
		}
}

// Render the record
$tb_locations->RowType = EW_ROWTYPE_EDIT; // Render as edit
RenderRow();
?>
<?php include "header.php" ?>
<script type="text/javascript">
<!--
var EW_PAGE_ID = "edit"; // Page id

//-->
</script>
<script type="text/javascript">
<!--

function ew_ValidateForm(fobj) {
	if (fobj.a_confirm && fobj.a_confirm.value == "F")
		return true;
	var i, elm, aelm, infix;
	var rowcnt = (fobj.key_count) ? Number(fobj.key_count.value) : 1;
	for (i=0; i<rowcnt; i++) {
		infix = (fobj.key_count) ? String(i+1) : "";
		elm = fobj.elements["x" + infix + "_LocationName"];
		if (elm && !ew_HasValue(elm)) {
			if (!ew_OnError(elm, "Please enter required field - Location Name"))
				return false;
		}
		elm = fobj.elements["x" + infix + "_LocationRent"];
		if (elm && !ew_CheckNumber(elm.value)) {
			if (!ew_OnError(elm, "Incorrect floating point number - Location Rent"))
				return false; 
		}
		elm = fobj.elements["x" + infix + "_PayRoll"];
		if (elm && !ew_CheckNumber(elm.value)) {
			if (!ew_OnError(elm, "Incorrect floating point number - Pay Roll"))
				return false; 
		}
		elm = fobj.elements["x" + infix + "_Utilities"];
		if (elm && !ew_CheckNumber(elm.value)) {
			if (!ew_OnError(elm, "Incorrect floating point number - Utilities"))
				return false; 
		}
	}
	return true;
}

//-->
</script>
<script type="text/javascript">
<!--
var ew_DHTMLEditors = [];

//-->
</script>
<script type="text/javascript">
<!--
var ew_MultiPagePage = "Page"; // multi-page Page Text
var ew_MultiPageOf = "of"; // multi-page Of Text
var ew_MultiPagePrev = "Prev"; // multi-page Prev Text
var ew_MultiPageNext = "Next"; // multi-page Next Text

//-->
</script>
<script language="JavaScript" type="text/javascript">
<!--

// Write your client script here, no need to add script tags.
// To include another .js script, use:
// ew_ClientScriptInclude("my_javascript.js"); 
//-->

</script>
<p><span class="phpmaker">Edit TABLE: Locations<br><br><a href="<?php echo $tb_locations->getReturnUrl() ?>">Go Back</a></span></p>
<?php
if (@$_SESSION[EW_SESSION_MESSAGE] <> "") {
?>
<p><span class="ewmsg"><?php echo $_SESSION[EW_SESSION_MESSAGE] ?></span></p>
<?php
	$_SESSION[EW_SESSION_MESSAGE] = ""; // Clear message
}
?>
<form name="ftb_locationsedit" id="ftb_locationsedit" action="tb_locationsedit.php" method="post" onSubmit="return ew_ValidateForm(this);">
<p>
<input type="hidden" name="a_edit" id="a_edit" value="U">
<table class="ewTable">
	<tr class="ewTableRow">
		<td class="ewTableHeader">ID</td>
		<td<?php echo $tb_locations->ID->CellAttributes() ?>><span id="cb_x_ID">
<div<?php echo $tb_locations->ID->ViewAttributes() ?>><?php echo $tb_locations->ID->EditValue ?></div>
<input type="hidden" name="x_ID" id="x_ID" value="<?php echo ew_HtmlEncode($tb_locations->ID->CurrentValue) ?>">
</span></td>
	</tr>
	<tr class="ewTableAltRow">
		<td class="ewTableHeader">Location Name<span class='ewmsg'>&nbsp;*</span></td>
		<td<?php echo $tb_locations->LocationName->CellAttributes() ?>><span id="cb_x_LocationName">
<input type="text" name="x_LocationName" id="x_LocationName" title="" size="30" maxlength="45" value="<?php echo $tb_locations->LocationName->EditValue ?>"<?php echo $tb_locations->LocationName->EditAttributes() ?>>
</span></td>
	</tr>
	<tr class="ewTableRow">
		<td class="ewTableHeader">Location Rent</td>
		<td<?php echo $tb_locations->LocationRent->CellAttributes() ?>><span id="cb_x_LocationRent">
<input type="text" name="x_LocationRent" id="x_LocationRent" title="" size="30" value="<?php echo $tb_locations->LocationRent->EditValue ?>"<?php echo $tb_locations->LocationRent->EditAttributes() ?>>
</span></td>
	</tr>
	<tr class="ewTableAltRow">
		<td class="ewTableHeader">Pay Roll</td>
		<td<?php echo $tb_locations->PayRoll->CellAttributes() ?>><span id="cb_x_PayRoll">
<input type="text" name="x_PayRoll" id="x_PayRoll" title="" size="30" value="<?php echo $tb_locations->PayRoll->EditValue ?>"<?php echo $tb_locations->PayRoll->EditAttributes() ?>>
</span></td>
	</tr>
	<tr class="ewTableRow">
		<td class="ewTableHeader">Utilities</td>
		<td<?php echo $tb_locations->Utilities->CellAttributes() ?>><span id="cb_x_Utilities">
<input type="text" name="x_Utilities" id="x_Utilities" title="" size="30" value="<?php echo $tb_locations->Utilities->EditValue ?>"<?php echo $tb_locations->Utilities->EditAttributes() ?>>
</span></td>
	</tr>
</table>
<p>
<input type="submit" name="btnAction" id="btnAction" value="   Edit   ">
</form>
<script language="JavaScript" type="text/javascript">
<!--

// Write your table-specific startup script here
// document.write("page loaded");
//-->

</script>
<?php include "footer.php" ?>
<?php

// If control is passed here, simply terminate the page without redirect
Page_Terminate();

// -----------------------------------------------------------------
//  Subroutine Page_Terminate
//  - called when exit page
//  - clean up connection and objects
//  - if url specified, redirect to url, otherwise end response
function Page_Terminate($url = "") {
	global $conn;

	// Page unload event, used in current page
	Page_Unload();

	// Global page unloaded event (in userfn*.php)
	Page_Unloaded();

	 // Close Connection
	$conn->Close();

	// Go to url if specified
	if ($url <> "") {
		ob_end_clean();
		header("Location: $url");
	}
	exit();
}
?>
<?php

// Load form values
function LoadFormValues() {

	// Load from form
	global $objForm, $tb_locations;
	$tb_locations->ID->setFormValue($objForm->GetValue("x_ID"));
	$tb_locations->LocationName->setFormValue($objForm->GetValue("x_LocationName"));
	$tb_locations->LocationRent->setFormValue($objForm->GetValue("x_LocationRent"));
	$tb_locations->PayRoll->setFormValue($objForm->GetValue("x_PayRoll"));
	$tb_locations->Utilities->setFormValue($objForm->GetValue("x_Utilities"));
}

// Restore form values
function RestoreFormValues() {
	global $tb_locations;
	$tb_locations->ID->CurrentValue = $tb_locations->ID->FormValue;
	$tb_locations->LocationName->CurrentValue = $tb_locations->LocationName->FormValue;
	$tb_locations->LocationRent->CurrentValue = $tb_locations->LocationRent->FormValue;
	$tb_locations->PayRoll->CurrentValue = $tb_locations->PayRoll->FormValue;
	$tb_locations->Utilities->CurrentValue = $tb_locations->Utilities->FormValue;
}
?>
<?php

// Load row based on key values
function LoadRow() {
	global $conn, $Security, $tb_locations;
	$sFilter = $tb_locations->SqlKeyFilter();
	if (!is_numeric($tb_locations->ID->CurrentValue)) {
		return FALSE; // Invalid key, exit
	}
	$sFilter = str_replace("@ID@", ew_AdjustSql($tb_locations->ID->CurrentValue), $sFilter); // Replace key value

	// Call Row Selecting event
	$tb_locations->Row_Selecting($sFilter);

	// Load sql based on filter
	$tb_locations->CurrentFilter = $sFilter;
	$sSql = $tb_locations->SQL();
	if ($rs = $conn->Execute($sSql)) {
		if ($rs->EOF) {
			$LoadRow = FALSE;
		} else {
			$LoadRow = TRUE;
			$rs->MoveFirst();
			LoadRowValues($rs); // Load row values

			// Call Row Selected event
			$tb_locations->Row_Selected($rs);
		}
		$rs->Close();
	} else {
		$LoadRow = FALSE;
	}
	return $LoadRow;
}

// Load row values from recordset
function LoadRowValues(&$rs) {
	global $tb_locations;
	$tb_locations->ID->setDbValue($rs->fields('ID'));
	$tb_locations->LocationName->setDbValue($rs->fields('LocationName'));
	$tb_locations->LocationRent->setDbValue($rs->fields('LocationRent'));
	$tb_locations->PayRoll->setDbValue($rs->fields('PayRoll'));
	$tb_locations->Utilities->setDbValue($rs->fields('Utilities'));
	$tb_locations->CreateDate->setDbValue($rs->fields('CreateDate'));
}
?>
<?php

// Render row values based on field settings
function RenderRow() {
	global $conn, $Security, $tb_locations;

	// Call Row Rendering event
	$tb_locations->Row_Rendering();

	// Common render codes for all row types
	// ID

	$tb_locations->ID->CellCssStyle = "";
	$tb_locations->ID->CellCssClass = "";

	// LocationName
	$tb_locations->LocationName->CellCssStyle = "";
	$tb_locations->LocationName->CellCssClass = "";

	// LocationRent
	$tb_locations->LocationRent->CellCssStyle = "";
	$tb_locations->LocationRent->CellCssClass = "";

	// PayRoll
	$tb_locations->PayRoll->CellCssStyle = "";
	$tb_locations->PayRoll->CellCssClass = "";

	// Utilities
	$tb_locations->Utilities->CellCssStyle = "";
	$tb_locations->Utilities->CellCssClass = "";
	if ($tb_locations->RowType == EW_ROWTYPE_VIEW) { // View row
	} elseif ($tb_locations->RowType == EW_ROWTYPE_ADD) { // Add row
	} elseif ($tb_locations->RowType == EW_ROWTYPE_EDIT) { // Edit row

		// ID
		$tb_locations->ID->EditCustomAttributes = "";
		$tb_locations->ID->EditValue = $tb_locations->ID->CurrentValue;
		$tb_locations->ID->CssStyle = "";
		$tb_locations->ID->CssClass = "";
		$tb_locations->ID->ViewCustomAttributes = "";

		// LocationName
		$tb_locations->LocationName->EditCustomAttributes = "";
		$tb_locations->LocationName->EditValue = ew_HtmlEncode($tb_locations->LocationName->CurrentValue);

		// LocationRent
		$tb_locations->LocationRent->EditCustomAttributes = "";
		$tb_locations->LocationRent->EditValue = $tb_locations->LocationRent->CurrentValue;

		// PayRoll
		$tb_locations->PayRoll->EditCustomAttributes = "";
		$tb_locations->PayRoll->EditValue = $tb_locations->PayRoll->CurrentValue;

		// Utilities
		$tb_locations->Utilities->EditCustomAttributes = "";
		$tb_locations->Utilities->EditValue = $tb_locations->Utilities->CurrentValue;
	} elseif ($tb_locations->RowType == EW_ROWTYPE_SEARCH) { // Search row
	}

	// Call Row Rendered event
	$tb_locations->Row_Rendered();
}
?>
<?php

// Update record based on key values
function EditRow() {
	global $conn, $Security, $tb_locations;
	$sFilter = $tb_locations->SqlKeyFilter();
	if (!is_numeric($tb_locations->ID->CurrentValue)) {
		return FALSE;
	}
	$sFilter = str_replace("@ID@", ew_AdjustSql($tb_locations->ID->CurrentValue), $sFilter); // Replace key value
	$tb_locations->CurrentFilter = $sFilter;
	$sSql = $tb_locations->SQL();
	$conn->raiseErrorFn = 'ew_ErrorFn';
	$rs = $conn->Execute($sSql);
	$conn->raiseErrorFn = '';
	if ($rs === FALSE)
		return FALSE;
	if ($rs->EOF) {
		$EditRow = FALSE; // Update Failed
	} else {

		// Save old values
		$rsold =& $rs->fields;
		$rsnew = array();

		// Field ID
		// Field LocationName

		$tb_locations->LocationName->SetDbValueDef($tb_locations->LocationName->CurrentValue, NULL);
		$rsnew['LocationName'] =& $tb_locations->LocationName->DbValue;

		// Field LocationRent
		$tb_locations->LocationRent->SetDbValueDef($tb_locations->LocationRent->CurrentValue, NULL);
		$rsnew['LocationRent'] =& $tb_locations->LocationRent->DbValue;

		// Field PayRoll
		$tb_locations->PayRoll->SetDbValueDef($tb_locations->PayRoll->CurrentValue, NULL);
		$rsnew['PayRoll'] =& $tb_locations->PayRoll->DbValue;

		// Field Utilities
		$tb_locations->Utilities->SetDbValueDef($tb_locations->Utilities->CurrentValue, NULL);
		$rsnew['Utilities'] =& $tb_locations->Utilities->DbValue;

		// Call Row Updating event
		$bUpdateRow = $tb_locations->Row_Updating($rsold, $rsnew);
		if ($bUpdateRow) {
			$conn->raiseErrorFn = 'ew_ErrorFn';
			$EditRow = $conn->Execute($tb_locations->UpdateSQL($rsnew));
			$conn->raiseErrorFn = '';
		} else {
			if ($tb_locations->CancelMessage <> "") {
				$_SESSION[EW_SESSION_MESSAGE] = $tb_locations->CancelMessage;
				$tb_locations->CancelMessage = "";
			} else {
				$_SESSION[EW_SESSION_MESSAGE] = "Update cancelled";
			}
			$EditRow = FALSE;
		}
	}

	// Call Row Updated event
	if ($EditRow) {
		$tb_locations->Row_Updated($rsold, $rsnew);
	}
	$rs->Close();
	return $EditRow;
}
?>
<?php

// Page Load event
function Page_Load() {

	//echo "Page Load";
}

// Page Unload event
function Page_Unload() {

	//echo "Page Unload";
}
?>
